const express = require("express")
const app = express();
const path = require("path");


app.use(express.urlencoded())
app.use(express.json())


app.get("/axiosTwo", (req, res) => {

  const {
    userId
  } = req.query;


  if (userId !== "001") {
    return res.send({
      code: 10001,
      msg: "查询失败",
      data: null
    })
  }



  res.send({
    code: 10000,
    msg: "查询成功",
    data: {
      name: "老李",
      age: 18,
      sex: "男"
    }
  })

})



app.get("/userName", (req, res) => {

    const {
      userId
    } = req.query;
  
  
    if (userId !== "001") {
      return res.send({
        code: 10001,
        msg: "查询失败",
        data: null
      })
    }
  
  
  
    res.send({
      code: 10000,
      msg: "查询成功",
      data: {
        name: "小红",
        age: 20,
        sex: "女"
      }
    })
  
  })


app.post("/newPoints", (req, res) => {
  const {
    name,
    age,
    sex
  } = req.body;

  if (!name || !age || !sex) {
    return res.send({
      code: 10001,
      mes: "添加失败",
      data: null
    })
  }

  res.send({
    code: 10000,
    mes: "添加成功",
    data: null
  })
})


app.put("/upTow", (req, res) => {
  const {
    userId,
    age,
    sex
  } = req.body;



  res.send({
    code: 10000,
    mes: "更新成功",
    data: null
  })
})

app.delete("/deleteTow", (req, res) => {
  const {
    userId
  } = req.query;
  

  res.send({
    code: 10000,
    mes: "删除成功",
    data: null
  })
})




app.get("/1", (req, res) => {
  const filePath = path.resolve(__dirname, "./axios-基础使用2.html");
  res.sendFile(filePath);
})

app.get("/2", (req, res) => {
  const filePath = path.resolve(__dirname, "./axios-请求别名2.html"); 
  res.sendFile(filePath);
})





app.listen(4040, () => {
  console.log("服务器请求成功 请访问 http://127.0.0.1:4040");
})